package com.carlinfo.shop.head.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.carlinfo.shop.common.util.ConstatFinalUtil;
import com.carlinfo.shop.common.web.controller.BaseController;
import com.carlinfo.shop.goods.pojo.AUsersCar;
import com.carlinfo.shop.goods.service.IGoodsService;
import com.carlinfo.shop.orders.pojo.AOrders;
import com.carlinfo.shop.orders.pojo.AOrdersGoods;
import com.carlinfo.shop.orders.service.IOrdersService;
import com.carlinfo.shop.users.pojo.AUsers;
import com.carlinfo.shop.users.pojo.AUsersAddress;
import com.carlinfo.shop.users.service.IUsersShopService;

/**
 * 订单相关的Controller
 * @author wangshMac
 */
@Controller
@RequestMapping("/head/orders")
public class OrdersHeadController extends BaseController
{
	@Resource
	private IUsersShopService usersShopService;
	@Resource
	private IGoodsService goodsService;
	@Resource
	private IOrdersService ordersService;
	
	/**
	 * 打开订单添加页面(下单操作)
	 * @return
	 */
	@RequestMapping("/ordersInsert")
	public String ordersInsert(HttpSession session,String[] usersCarIds,Model model)
	{
		ConstatFinalUtil.LOGGER.info("====ordersInsert====");
		AUsers users = (AUsers) session.getAttribute("users");
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 查询收货地址列表 */
		/* 根据用户id和状态查询收货地址列表 */
		condMap.put("usersId", users.getId());
		condMap.put("status", "1");
		Map<String, Object> resultMap = this.usersShopService.findCondListUsersAddressService(null, condMap);
		List<AUsersAddress> usersAddressList = (List<AUsersAddress>) resultMap.get("list");
		model.addAttribute("usersAddressList", usersAddressList);
		
		/* 要查询用户选择的购物车列表 */
		if(usersCarIds != null)
		{
			List<AUsersCar> usersCarList = new ArrayList<AUsersCar>();
			for (int i = 0; i < usersCarIds.length; i++)
			{
				String usersCarId = usersCarIds[i];
				/* 根据id查询用户的购物车 */
				condMap.clear();
				condMap.put("id", usersCarId);
				AUsersCar usersCar = this.goodsService.findOneUsersCarService(condMap);
				usersCarList.add(usersCar);
			}
			
			model.addAttribute("usersCarList", usersCarList);
		}
		return "/head/orders/ordersInsert";
	}
	
	/**
	 * 打开订单添加页面(下单操作)
	 * 
	 * 网页赋值的操作:收货地址id,note
	 * 购物车的id:可以获取到商品和数量
	 * 暂时不考虑的有:支付方式
	 * @return
	 */
	@RequestMapping("/ordersInsertSubmit")
	public String ordersInsertSubmit(AOrders orders,String [] usersCarIds,HttpSession session)
	{
		ConstatFinalUtil.LOGGER.info("====ordersInsertSubmit====");
		/* 获取用户的id */
		AUsers users = (AUsers) session.getAttribute("users");
		
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 要处理一个购物车 
		 * 是为了生成商品明细
		 * */
		if(usersCarIds != null)
		{
			List<AOrdersGoods> ordersGoodsList = new ArrayList<AOrdersGoods>();
			for (int i = 0; i < usersCarIds.length; i++)
			{
				String usersCarId = usersCarIds[i];
				/* 根据id查询用户的购物车 */
				condMap.clear();
				condMap.put("id", usersCarId);
				AUsersCar usersCar = this.goodsService.findOneUsersCarService(condMap);
				
				AOrdersGoods ordersGoods = new AOrdersGoods() ; 
				/* 为订单明细赋值 */
				ordersGoods.setGoodsId(usersCar.getGoodsId());
				ordersGoods.setNum(usersCar.getNum());
				ordersGoodsList.add(ordersGoods);
			}
			
			orders.setOrdersGoodList(ordersGoodsList);
			/* 所有赋值的时候统一交给Service,因为Service有事务管理器 */
			orders.setCreateTime(new Date());
			orders.setUpdateTime(new Date());
			orders.setPubTime(new Date());
			orders.setUsersId(users.getId());
			JSONObject resDbJSON = this.ordersService.saveOneOrdersService(orders);
			ConstatFinalUtil.LOGGER.info("保存订单结果:{}",resDbJSON);
		}
		return "/head/orders/ordersInsert";
	}
}

